//
//  HomePageViewController.m
//  WuShiCity
//
//  Created by chengjie on 16/7/4.
//  Copyright © 2016年 chengjie. All rights reserved.
//

#import "HomePageViewController.h"
#import "HeaderView.h"
#import "CollectionFootView.h"
#import "CollectionReusableView.h"
#import "CollectionViewCell.h"
#import "LineLayout.h"
#import "HomePageCollectionViewController.h"
#import "HPChoiceButtonView.h"
#import "HomScrollCollectionReusableView.h"

@interface HomePageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic,strong) HPChoiceButtonView *hpChoiceBV;
@end

@implementation HomePageViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.view addSubview: self.collectionView];
  // 隐藏导航栏
    self.navigationController.navigationBarHidden = YES;
  // 获取自定义view
    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:nil options:nil];
    HeaderView *cv =[nibView objectAtIndex:0];
    cv.frame = CGRectMake(0, 0, self.view.frame.size.width, 150);
    cv.naVC = self.navigationController; //获取导航栏
       [self.view addSubview:cv];
    

    
}


#pragma mark ---collectionView代理方法--

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (section == 0) {
        return 10;
    }else if (section == 1){
        
        return 3;
    }else if (section == 2){
        
        return 4;
    }


    return 0;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 7;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
        cell.myLabel.text = @"品牌";
        return cell;

    }else if (indexPath.section == 1){
        if (indexPath.row == 0) {
            
            CollectionViewCell1 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell1" forIndexPath:indexPath];
            cell.nameLabel.text = @"品牌";
            return cell;
        }else{
            
            CollectionViewCelltwo *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCelltwo" forIndexPath:indexPath];
            cell.nameLabel.text = @"标题";
            return cell;
        }
       

    }else if (indexPath.section == 2){
        
        
        CollectionViewCell3 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell3" forIndexPath:indexPath];
//        cell.nameLabel.text = @"标题";
        return cell;
        
    }
//    else if (indexPath.section == 3){
//        
//        
//        CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
//        //        cell.nameLabel.text = @"标题";
//        return nil;
//    }

    return nil;
   }



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%ld",(long)indexPath.row);
    
    
}

//item大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake((kScreenWidth)/6, (kScreenWidth)/6*2);
       
    }else if (indexPath.section == 1){
        return CGSizeMake((kScreenWidth)/3, (kScreenWidth)/3);
    }else if (indexPath.section == 2){
        return CGSizeMake((kScreenWidth)/2, (kScreenWidth)/5);
    }
    //    else if (indexPath.section == 3){
    //
    //    }
    else {
        
        return CGSizeMake(0, 0);
        
    }
}

//头部视图大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(CGRectGetWidth(self.view.frame), 50);
        
    } else if (section == 3){
        
        return CGSizeMake(CGRectGetWidth(self.view.frame), 50);
    }else{
        return CGSizeMake(0, 0);

    }
    
}

//定义尾视图的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section == 0) {
        
        return CGSizeMake(CGRectGetWidth(self.view.frame), 50);
    }else if (section == 3){
      return CGSizeMake(kScreenWidth, 200);

    }else {
        
      return CGSizeMake(0, 0);
    }
    
}

//item大小布局
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section == 0) {
       return  UIEdgeInsetsMake(0, 10,0,10);
    }else if (section == 1){
        
        return  UIEdgeInsetsMake(0, 0,0,0);

    }
    else {
        return UIEdgeInsetsMake(0, 0, 10, 0);
    }
    
}







//头／尾视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0)
    {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            
            CollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionReusableView" forIndexPath:indexPath];
            
            headView.titleLabel.text = @"专区";
            return headView;
            
        }else{
            
            CollectionFootView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"CollectionFootView" forIndexPath:indexPath];
            footView.titleLabel.text = @"广告";
            return footView;
        }
    }if (indexPath.section == 3)
    {
        if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
            
            CollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionReusableView" forIndexPath:indexPath];
            
            headView.titleLabel.text = @"工具";
            
            return headView;
        }else if([kind isEqualToString:UICollectionElementKindSectionFooter]){
            NSMutableArray *mArray = [[NSMutableArray alloc] init];
            for (int i = 0; i < 23; i++) {
                
                UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d%d.jpg",i,i]];
                [mArray addObject:image];
            }
            
            NSMutableArray *labelArr = [NSMutableArray arrayWithObjects:@"as", @"as",@"as",@"as",@"as",@"as",@"as", @"as",@"as",@"as",@"as",@"as",@"as", @"as",@"as",@"as",@"as",@"as",@"as", @"as", @"as",@"as",@"as",nil];
            
            //            UICollectionReusableView *footview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
            //            // 创建布局对象
            //            UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
            //
            //            HomePageCollectionViewController *hpcVC = [[HomePageCollectionViewController alloc] initWithCollectionViewLayout:flowLayout];
            //
            //            if (kScreenWidth >= 375) {
            //                hpcVC.collectionView.frame = CGRectMake(0, footview.frame.origin.y, 0, 100);
            //            }else{
            //                hpcVC.collectionView.frame = CGRectMake(0, footview.frame.origin.y, 0, 100);
            //            }
            //
            //            [footview addSubview:hpcVC.collectionView];
            //            view.backgroundColor = [UIColor redColor];
            
            //            [footview addSubview:[self setTableHeaderView]];
            //            footview.backgroundColor = [UIColor redColor];
            //            return footview;
            
            HomScrollCollectionReusableView *scrollFooter = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ScrollFooter" forIndexPath:indexPath];
//            scrollFooter.backgroundColor = [UIColor redColor];
            [scrollFooter initWithFrame:CGRectMake(0, 0, scrollFooter.frame.size.width, scrollFooter.frame.size.height) titlesArray:labelArr btnsArray:mArray];
            return scrollFooter;
        }
    }
    return nil;
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark ---懒加载--
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        loyout = [[UICollectionViewFlowLayout alloc] init];
        loyout.minimumInteritemSpacing = 0;
        loyout.minimumLineSpacing = 0;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 120, kScreenWidth, kScreenHeight-120) collectionViewLayout:loyout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        //         自定义item
        [_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell"];
        [_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell1" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell1"];
        [_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCelltwo" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCelltwo"];
        [_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell3" bundle:nil] forCellWithReuseIdentifier:@"CollectionViewCell3"];
        
        
        //        头视图／尾视图
        [_collectionView registerNib:[UINib nibWithNibName:@"CollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CollectionReusableView"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"CollectionFootView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"CollectionFootView"];
//        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
        [_collectionView registerClass:[HomScrollCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ScrollFooter"];
    }
    return _collectionView;
    
}


#pragma mark -- 表头collection设置
//表头collection设置
- (UIView *)setTableHeaderView
{
    // 创建布局对象
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    HomePageCollectionViewController *hpcVC = [[HomePageCollectionViewController alloc] initWithCollectionViewLayout:flowLayout];
    
    UIView *view = [[UIView alloc] init];
    
    if (kScreenWidth >= 375) {
        hpcVC.collectionView.frame = CGRectMake(0, 0, 0, 150);
        view.frame = CGRectMake(0, 0, 0, 194);
        
    }else{
        hpcVC.collectionView.frame = CGRectMake(0, 0, 0, 140);
        view.frame = CGRectMake(0, 0, 0, 184);
    }
    [view addSubview:hpcVC.collectionView];
    view.backgroundColor = [UIColor redColor];
    
    return view;
    
}



@end
